package com.cloudsigns.factorymanager2.engineering.model;

import android.text.TextUtils;

import com.cloudsigns.factorymanager2.MyApplicaton;

import com.cloudsigns.factorymanager2.engineering.bean.EngineeringCompletionOrderDetailBean;

import com.cloudsigns.factorymanager2.engineering.utils.EngineeringUtil;

import com.cloudsigns.factorymanager2.project.utils.ConstantUtil;
import com.cloudsigns.factorymanager2.project.utils.ProjectUtil;

import org.apache.http.HttpResponse;
import org.apache.http.client.HttpClient;
import org.apache.http.client.methods.HttpGet;
import org.apache.http.impl.client.DefaultHttpClient;
import org.apache.http.protocol.HTTP;
import org.apache.http.util.EntityUtils;
import org.json.JSONArray;
import org.json.JSONObject;

import java.util.ArrayList;
import java.util.List;

/**
 * [ [通用]获取工程部竣工订单详情 ]
 * Created by tony on 2018/5/11.
 */

public class EngineeringCompletionOrderDetailModel implements IEngineeringCompletionOrderDetailModel {

    @Override
    public void getEngineeringCompletionOrderDetail(final String orderId, final EngineeringCompletionOrderDetailListener engineeringCompletionOrderDetailListener) {
        new Thread(new Runnable() {
            @Override
            public void run() {

                try {
                    String url = ProjectUtil.ENGINEERING_COMPLETION_ORDER_DETAIL + "?orderId=" + orderId;
                    HttpClient client = new DefaultHttpClient();
                    HttpGet httpGet = new HttpGet(url);

                    HttpResponse response;

                    httpGet.addHeader("token", MyApplicaton.getApplication().getToken());
                    httpGet.addHeader(HTTP.CONTENT_TYPE, ConstantUtil.CONTENT_TYPE);


                    response = client.execute(httpGet);
                    String result = EntityUtils.toString(response.getEntity(), "UTF-8").trim();
                    JSONObject json = new JSONObject(result);
                    String code = json.getString("code");
                    if (null != engineeringCompletionOrderDetailListener) {
                        if (code.equals(EngineeringUtil.HTTP_SUCCESSED_COD)) {
                            JSONObject dataJsonObj = new JSONObject(json.getString("data"));
                            EngineeringCompletionOrderDetailBean orderBean = new EngineeringCompletionOrderDetailBean();

                            String completedImgUrlListJsonArrStr = dataJsonObj.getString("completedImgUrlList");
                            if (null != completedImgUrlListJsonArrStr && !completedImgUrlListJsonArrStr.equals("null") && !TextUtils.isEmpty(completedImgUrlListJsonArrStr)) {
                                JSONArray completedImgUrlListJsonArr = new JSONArray(completedImgUrlListJsonArrStr);
                                List<String> completedImgUrlList = new ArrayList<String>();
                                for (int j = 0; j < completedImgUrlListJsonArr.length(); j++) {
                                    String str = (String) completedImgUrlListJsonArr.get(j);
                                    completedImgUrlList.add(str);
                                }
                                orderBean.setCompletedImgUrlList(completedImgUrlList);
                            }

                            orderBean.setCreateEvaluateInfoAccountId(dataJsonObj.getString("createEvaluateInfoAccountId"));
                            orderBean.setCreateEvaluateInfoAccountName(dataJsonObj.getString("createEvaluateInfoAccountName"));

                            String createEvaluateInfoTimeStr = dataJsonObj.getString("createEvaluateInfoTime");
                            if (!TextUtils.isEmpty(createEvaluateInfoTimeStr) && !createEvaluateInfoTimeStr.equals("null")) {
                                orderBean.setCreateEvaluateInfoTime(Long.parseLong(createEvaluateInfoTimeStr));
                            }

                            orderBean.setCreateOrderAccountId(dataJsonObj.getString("createOrderAccountId"));
                            orderBean.setCreateOrderAccountName(dataJsonObj.getString("createOrderAccountName"));

                            String createOrderTimeStr = dataJsonObj.getString("createOrderTime");
                            if (!TextUtils.isEmpty(createOrderTimeStr) && !createOrderTimeStr.equals("null")) {
                                orderBean.setCreateOrderTime(Long.parseLong(createOrderTimeStr));
                            }

                            orderBean.setEngineeringName(dataJsonObj.getString("engineeringName"));
                            orderBean.setEvaluateInfo(dataJsonObj.getString("evaluateInfo"));

                            String featureImgUrlListJsonArrStr = dataJsonObj.getString("featureImgUrlList");
                            if (null != featureImgUrlListJsonArrStr && !featureImgUrlListJsonArrStr.equals("null") && !TextUtils.isEmpty(featureImgUrlListJsonArrStr)) {
                                JSONArray featureImgUrlListJsonArr = new JSONArray(featureImgUrlListJsonArrStr);
                                List<String> featureImgUrlList = new ArrayList<String>();
                                for (int j = 0; j < featureImgUrlListJsonArr.length(); j++) {
                                    String str = (String) featureImgUrlListJsonArr.get(j);
                                    featureImgUrlList.add(str);
                                }
                                orderBean.setFeatureImgUrlList(featureImgUrlList);
                            }

                            orderBean.setId(dataJsonObj.getString("id"));
                            orderBean.setOrderId(dataJsonObj.getString("orderId"));

                            String otherImgUrlListJsonArrStr = dataJsonObj.getString("otherImgUrlList");
                            if (null != otherImgUrlListJsonArrStr && !otherImgUrlListJsonArrStr.equals("null") && !TextUtils.isEmpty(otherImgUrlListJsonArrStr)) {
                                JSONArray otherImgUrlListJsonArr = new JSONArray(otherImgUrlListJsonArrStr);
                                List<String> otherImgUrlList = new ArrayList<String>();
                                for (int j = 0; j < otherImgUrlListJsonArr.length(); j++) {
                                    String str = (String) otherImgUrlListJsonArr.get(j);
                                    otherImgUrlList.add(str);
                                }
                                orderBean.setOtherImgUrlList(otherImgUrlList);
                            }

                            orderBean.setProjectName(dataJsonObj.getString("projectName"));



                            engineeringCompletionOrderDetailListener.getEngineeringCompletionOrderDetailSuccessed(orderBean);

                        } else {
                            String error_str = code;
                            error_str += "," + ConstantUtil.getErrorCode(code);
                            if (!json.isNull("error_str")) {
                                error_str += "," + json.getString("error_str");
                            }
                            engineeringCompletionOrderDetailListener.getEngineeringCompletionOrderDetailFailed(error_str);
                        }
                    }
                } catch (Exception e) {
                    e.printStackTrace();
                } finally {

                }
            }
        }).start();
    }

    public interface EngineeringCompletionOrderDetailListener {
        void getEngineeringCompletionOrderDetailSuccessed(EngineeringCompletionOrderDetailBean engineeringCompletionOrderDetailBean);

        void getEngineeringCompletionOrderDetailFailed(String desc);
    }
}
